﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// 出库主表实体类，对应[dbo].[wh_outbound]表
    /// </summary>
    public class WhOutbound
    {
        /// <summary>
        /// 出库订单ID
        /// </summary>
        [Key]
        public long StockOutOrderId { get; set; }

        /// <summary>
        /// 出库订单编号
        /// </summary>
        public string StockOutOrderNo { get; set; }

        /// <summary>
        /// 仓库ID
        /// </summary>
        public long WarehouseId { get; set; }

        /// <summary>
        /// 源订单编号
        /// </summary>
        public string SourceOrderNo { get; set; }

        /// <summary>
        /// 出库类型
        /// </summary>
        public string StockOutType { get; set; }

        /// <summary>
        /// 出库日期
        /// </summary>
        public DateTime StockOutDate { get; set; }

        /// <summary>
        /// 出库仓库ID
        /// </summary>
        public long OutWarehouseId { get; set; }

        /// <summary>
        /// 操作人ID
        /// </summary>
        public long OperatorBy { get; set; }

        /// <summary>
        /// 操作日期
        /// </summary>
        public DateTime OperatorDate { get; set; }

        /// <summary>
        /// 订单状态
        /// </summary>
        public string OrderStatus { get; set; }

        /// <summary>
        /// 审核人ID
        /// </summary>
        public long? AuditorBy { get; set; }

        /// <summary>
        /// 审核时间
        /// </summary>
        public DateTime? AuditorTime { get; set; }

        /// <summary>
        /// 审核意见
        /// </summary>
        public string AuditorComment { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        public long CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 更新人ID
        /// </summary>
        public long UpdateBy { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }
    }
    /// <summary>
    /// 出库单详细信息实体类
    /// </summary>
    public class StockOutDetailDto
    {
        public long StockOutOrderId { get; set; }
        /// <summary>
        /// 出库单号
        /// </summary>
        public string StockOutOrderNo { get; set; }

        /// <summary>
        /// 出库类型
        /// </summary>
        public string StockOutType { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 出库日期
        /// </summary>
        public DateTime? StockOutDate { get; set; }

        /// <summary>
        /// 仓库名称
        /// </summary>
        public string WarehouseName { get; set; }

        /// <summary>
        /// 关联单号
        /// </summary>
        public string SourceOrderNo { get; set; }

        /// <summary>
        /// 客户名称
        /// </summary>
        public string CustomerName { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string ContactPerson { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string ContactPhone { get; set; }

        /// <summary>
        /// 出库货品
        /// </summary>
        public string GoodsName { get; set; }


        /// <summary>
        /// 出库数量
        /// </summary>
        public decimal? OutStockQuantity { get; set; }

        /// <summary>
        /// 出库金额
        /// </summary>
        public decimal? OutStockAmount { get; set; }

        /// <summary>
        /// 经办人
        /// </summary>
        public string OperatorName { get; set; }

        /// <summary>
        /// 制单人
        /// </summary>
        public string CreatorName { get; set; }

        /// <summary>
        /// 所在部门
        /// </summary>
        public string DepartmentName { get; set; }

        /// <summary>
        /// 制单时间
        /// </summary>
        public DateTime? CreateTime { get; set; }
        /// <summary>
        /// 审核人ID
        /// </summary>
        public string AuditorName { get; set; }

        /// <summary>
        /// 审核时间
        /// </summary>
        public DateTime? AuditorTime { get; set; }
    }

    /// <summary>
    /// 出库单汇总信息（按出库单分组）
    /// </summary>
    public class StockOutSummaryDto
    {
        /// <summary>
        /// 出库单号
        /// </summary>
        public string StockOutOrderNo { get; set; }

        /// <summary>
        /// 出库类型
        /// </summary>
        public string StockOutType { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 出库日期
        /// </summary>
        public DateTime? StockOutDate { get; set; }

        /// <summary>
        /// 仓库名称
        /// </summary>
        public string WarehouseName { get; set; }

        /// <summary>
        /// 关联单号
        /// </summary>
        public string SourceOrderNo { get; set; }

        /// <summary>
        /// 客户名称
        /// </summary>
        public string CustomerName { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string ContactPerson { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string ContactPhone { get; set; }

        /// <summary>
        /// 经办人
        /// </summary>
        public string OperatorName { get; set; }

        /// <summary>
        /// 制单人
        /// </summary>
        public string CreatorName { get; set; }

        /// <summary>
        /// 所在部门
        /// </summary>
        public string DepartmentName { get; set; }

        /// <summary>
        /// 制单时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 出库明细列表
        /// </summary>
        public List<StockOutItemDto> Items { get; set; } = new List<StockOutItemDto>();

        /// <summary>
        /// 总出库数量
        /// </summary>
        public decimal? TotalQuantity => Items.Sum(x => x.OutStockQuantity);

        /// <summary>
        /// 总出库金额
        /// </summary>
        public decimal? TotalAmount => Items.Sum(x => x.OutStockAmount);
    }

    /// <summary>
    /// 出库单明细项
    /// </summary>
    public class StockOutItemDto
    {
        /// <summary>
        /// 出库货品
        /// </summary>
        public string GoodsName { get; set; }

        /// <summary>
        /// 出库数量
        /// </summary>
        public decimal? OutStockQuantity { get; set; }

        /// <summary>
        /// 出库单价
        /// </summary>
        public decimal? OutStockUnitPrice { get; set; }

        /// <summary>
        /// 出库金额
        /// </summary>
        public decimal? OutStockAmount { get; set; }

        /// <summary>
        /// 批次号
        /// </summary>
        public string BatchNo { get; set; }

        /// <summary>
        /// 生产日期
        /// </summary>
        public DateTime? ProductionDate { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }
    }

    /// <summary>
    /// 出库单查询参数
    /// </summary>
    public class StockOutQueryParams
    {
        /// <summary>
        /// 出库单号
        /// </summary>
        public string StockOutOrderNo { get; set; }

        /// <summary>
        /// 出库类型
        /// </summary>
        public string StockOutType { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public int? Status { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 仓库ID
        /// </summary>
        public int? WarehouseId { get; set; }

        /// <summary>
        /// 客户ID
        /// </summary>
        public int? CustomerId { get; set; }

        /// <summary>
        /// 页码
        /// </summary>
        public int PageIndex { get; set; } = 1;

        /// <summary>
        /// 页大小
        /// </summary>
        public int PageSize { get; set; } = 20;
    }
    public class SalesOrderAvailableGoodsDto
    {
        public long GoodsId { get; set; }
        public string GoodsCode { get; set; }
        public string GoodsName { get; set; }
        public string GoodsTypeName { get; set; }
        public string Specification { get; set; }
        public decimal AvailableStock { get; set; }
        // 移除批次相关字段
        public string WarehouseName { get; set; }  // 改为仓库名称
        public string StorageLocationName { get; set; }  // 库位名称
        public long InventoryId { get; set; }
        public long WarehouseId { get; set; }  // 保留仓库ID用于后端处理
        public long StorageLocationId { get; set; }  // 保留库位ID用于后端处理
        public decimal SalesQuantity { get; set; }
        public decimal OutboundQuantity { get; set; }
        public decimal NonOutboundQuantity { get; set; }
        public decimal OutboundUnitPrice { get; set; }
    }

    public class GoodsInventoryInfoDto
    {
        public long InventoryId { get; set; }
        public long BatchId { get; set; }
        public string BatchNo { get; set; }
        public decimal AvailableQuantity { get; set; }
        public decimal UnitPrice { get; set; }
        public long GoodsId { get; set; }
    }
    /// <summary>
    /// 创建出库单请求模型
    /// </summary>
    public class CreateOutboundRequest
    {
        public WhOutbound Outbound { get; set; }
        public List<WhOutboundDetail> Details { get; set; }
    }
    public class OutboundDetailDto
    {
        // 基础信息
        public string GoodsCode { get; set; }
        public string GoodsName { get; set; }
        public string Specification { get; set; }
        public long GoodsId { get; set; }
        public string GoodsTypeName { get; set; }

        // 数量信息
        public decimal SalesQuantity { get; set; }           // 销售数量
        public decimal OutboundQuantity { get; set; }        // 已出库数量
        public decimal NonOutboundQuantity { get; set; }     // 未出库数量
        public decimal AvailableInventoryQty { get; set; }   // 当前库存

        // 价格信息
        public decimal OutboundUnitPrice { get; set; }

        // 仓库信息
        public long WarehouseId { get; set; }

        // 用户输入字段
        public decimal CurrentOutStockQty { get; set; } // 本次出库数量
        public long StorageLocationId { get; set; }   // 选择的库位
        public string Unit { get;set; }
        public string Remarks { get; set; }

        // 前端使用的字段（不需要从后端获取）
        [NotMapped]
        public List<object> _locations { get; set; } // 前端自己获取的库位列表
    }
}